<?php

App::import('Sanitize');
class SellersController extends AppController
{
echo __METHOD__;
var $name = 'Sellers';
var $helpers = array('Html', 'Session');
var $components = array('Auth');
var $uses = array('User');
function beforeFilter(){
$this->Auth->allow(array( 'ajax_login',
 'confirmed_need',
 'login', 'logout',
 'sellerjoin',
 'index', 'store',
 'sellerinfo',
 'storefront',
 'get_brand_lock',
 'update_inventory'
)
);
$this->Auth->loginRedirect = array('controller' => 'Sellers', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'Sellers', 'action' => 'login');
$this->Auth->loginError = "You are not authorised!";
$this->Auth->authError = "Sorry, you are lacking access.";
parent::beforeFilter();
}
/* * ****************************************************Admin Dashboard	****************************************************************************** */
function index()
{
echo __METHOD__;
$this->redirect(array('action' => 'dashboard'));
}
function store($store_token="")
{
echo __METHOD__;
$this->layout = "default";
$seller = $this->Seller->findByMerchantToken($store_token);
$this->Set('seller', $seller);
$extra_conditions = "";
$this->paginate['MasterProduct'] =array('conditions' => 'MasterProduct.seller_id = "'.$seller['Seller']['id'].'" '.$extra_conditions.' ',
 'limit' => 20,
 'fields' => array('MasterProduct.product_id'),
 'order' => 'MasterProduct.created Desc'
);
$MasterProducts = $this->paginate('MasterProduct');
//pr($MasterProducts);
//exit;
if(isset($MasterProducts) and ($MasterProducts))
{
$opm = 0;
foreach($MasterProducts as $MasterProduct)
{
$product = $this->Product->findById($MasterProduct['MasterProduct']['product_id']);
if(isset($product) and ($product))
{
$products[$opm] = $product;
$opm++;
}
}
if(isset($products) and ($products))
{
$x =0;
foreach($products as $product)
{
$stock_detail = $this->_get_stock_position($product['Product']['stock']);
$products[$x]['Product']['stock_detail']= $stock_detail;
$MasterProduct = $this->__get_product_min_price($product['Product']['id']);
if(isset($MasterProduct) and ($MasterProduct) and sizeof($MasterProduct))
{
$products[$x]['Product']['MasterProduct'] = $MasterProduct;
$x++;
}
}
//pr($products);
//exit;		
$this->Set('products', $products);
}
}


$about_us_info = $this->SellerPage->find('all', array('conditions' => 'SellerPage.seller_token="'.$store_token.'" and SellerPage.type="aboutus"'));
//pr($about_us_info);
$this->Set('about_us_info', $about_us_info);
//exit;
}

function sellerinfo($store_token="")
{
echo __METHOD__;
$this->layout = "default";
$params = $this->params;
$this->Set('params', $params);
if(isset($store_token) and ($store_token))
$seller = $this->Seller->findByMerchantToken($store_token);
$this->Set('seller', $seller);

$about_us_info = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$store_token.'" and SellerPage.type="aboutus"'));
$this->Set('about_us_info', $about_us_info);

$Shipping = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$store_token.'" and SellerPage.type="seller_shipping"'));
$this->Set('Shipping', $Shipping);

$privacy_policy = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$store_token.'" and SellerPage.type="seller_privacy_policy"'));
$this->Set('privacy_policy', $privacy_policy);

$seller_faq = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$store_token.'" and SellerPage.type="seller_faq"'));
$this->Set('seller_faq', $seller_faq);

$seller_gift_services = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$store_token.'" and SellerPage.type="seller_gift_services"'));
$this->Set('seller_gift_services', $seller_gift_services);

$custom = $this->SellerPage->find('all', array('conditions' => 'SellerPage.seller_token="'.$store_token.'" and SellerPage.type="custom"'));
$this->Set('custom', $custom);
}

function dashboard()
{
echo __METHOD__;
$this->layout = "seller_home";
}


function sellerjoin()
{
echo __METHOD__;
$this->layout = "default";
if(isset($this->data) and ($this->data))
{
if((isset($this->data['User']['email']) and ($this->data['User']['email'])) and
($this->data['User']['email']==$this->data['User']['confirm_email']))
{
$subscribe_email = $this->data['User']['email'];
$userregistered = $this->User->find('all',
 array('conditions' => 'User.username = "'.$subscribe_email.'" 
															                         and 
																					User.email = "'.$subscribe_email.'"',
 'fields' => 'User.id'
)
);
if(isset($userregistered[0]['User']['id']) and sizeof($userregistered[0]['User']['id']) and ($userregistered[0]['User']['id']))
{
$this->Session->setFlash('Email already in use');
$this->render('sellerjoin');
}
else
{
$this->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
$this->data['User']['confirm_password'] = $this->Auth->password($this->data['User']['confirm_password']);
if(isset($this->data['User']['password'])
and
($this->data['User']['password']==$this->data['User']['confirm_password']))
{
$this->data['User']['username'] = $this->data['User']['email'];
$slug_value = str_replace(' ', '_', strtolower($this->data['User']['name']));
$User_Slug = $this->User->generateSlug($slug_value);
$this->data['User']['slug']=$User_Slug;
$this->data['User']['hash'] = $this->__generateKey('User');
//pr($this->data);
//exit;
$this->User->create();

$this->data['User']['last_login']=date('Y-m-d h:i:s');

$this->data['User']['is_seller'] = '1';

$this->data['User']['is_verified'] = '1';

$this->data['User']['conditions_agree'] = 1;

$this->data['User']['role'] = 'seller';

$this->User->save($this->data['User']);

$user_id = $this->User->getLastInsertId();

unset($this->data['User']['confirm_email']);

unset($this->data['User']['confirm_password']);

$this->data = $this->data['User'];

$this->Auth->login($this->data);

$this->Session->setFlash('Your account has been created Please fillout pending details to become verified Seller on WonderE');

$this->redirect(array('controller' => 'sellers', 'action' => "account_info"));
}
else
{
$this->Session->setFlash('Password are Not matched. Please try again.');
$this->render('sellerjoin');
}
}
}
else
{
$this->Session->setFlash('Email`s are not matched properly.');
$this->render('sellerjoin');
}

}
}

function account_info()
{
echo __METHOD__;
$this->layout = "default";
$user_id = $this->Auth->User('id');
$seller_account_info = $this->Seller->findByUserId($user_id);
$this->Set('seller_account_info', $seller_account_info);
$__list_countries = $this->__list_countries();
$this->Set('list_countries', $__list_countries);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['Seller']) and ($this->data['Seller']))
{
$this->data['Seller']['user_id'] = $this->Auth->User('id');
$existing = $this->Seller->find('first',
 array('conditions' => 'Seller.user_id = "'. $this->data['Seller']['user_id'].'"',
 'fields' => 'User.id'));
//pr($existing);
//exit;
if(isset($existing) and ($existing) and sizeof($existing))
{
$this->render('account_info');
}
else
{
$this->data['Seller']['is_verified'] = 0;
$this->data['Seller']['merchant_token'] = $this->__generateToken('Seller', 'merchant_token');
$this->Seller->Save($this->data['Seller']);
//$Seller_id = $this->Seller->getLastInsertId();
if(isset($this->data['Address']) and ($this->data['Address']))
{
$this->data['Address']['user_id'] = $this->Auth->User('id');
$this->Address->Save($this->data['Address']);
}
$this->redirect(array('controller' => 'sellers', 'action' => 'confirmed_need'));
}

}
elseif(isset($this->data['Paypal']) and ($this->data['Paypal']))
{
$this->Paypal->save($this->data['Paypal']);
$this->redirect(array('action' => 'seller_accountinfo'));
}
//$this->Session->write('seller_id', $Seller_id);
}
}

function identity_verification()
{
echo __METHOD__;
$this->layout = "default";
if(isset($this->data) and ($this->data))
{
if(isset($this->data['Seller']) and ($this->data['Seller']))
{
$this->data['Seller']['id'] = $this->Session->read('seller_id');
//pr( $this->data);
// exit;
foreach($this->data['Seller']['doc'] as $doc)
{
$name = strtolower(str_replace(' ', '_', $doc['doc_name']));
$doc_arr[] = $name.":".$doc['name'];
if(isset($doc) and ($doc)
and ($doc['name']))
{
$image = $doc;
$upload_file = $this->upload_img("", $image, 'Docs');
}
}
$doc_arr_imp = implode(", ", $doc_arr);
$this->data['Seller']['docs'] = $doc_arr_imp;
$this->Seller->save($this->data['Seller']);
$this->redirect(array('action' => 'confirmed_need'));
}
}

}

function confirmed_need()
{
echo __METHOD__;
$this->layout = "default";
}


function inventory($status="")
{
echo __METHOD__;
$Seller_id = $this->Auth->User('Seller_id');

if(isset($status) and ($status=="active"))
{
$extra_conditions = " and MasterProduct.qty>0";
}
else if(isset($status) and ($status=="Inactive"))
{
//pr($status);
//exit;
$extra_conditions = " and MasterProduct.qty=0";
}
else{
$extra_conditions = "";
}
$this->paginate['MasterProduct'] =array('conditions' => 'MasterProduct.seller_id = "'.$Seller_id.'" '.$extra_conditions.' ',
 'limit' => '20',
 'fields' => array('MasterProduct.product_id'),
 'order' => 'MasterProduct.id Desc'
);
$MasterProducts = $this->paginate('MasterProduct');
// pr($MasterProducts);
//exit;
if(isset($MasterProducts) and ($MasterProducts))
{
$op = 0;
foreach($MasterProducts as $MasterProduct)
{
$product = $this->Product->findById($MasterProduct['MasterProduct']['product_id']);
if(isset($product['Product']['id']) and ($product['Product']['id']))
{
$category = $this->Category->findById($product['Product']['cat_id']);
$product['Product']['Category'] = $category['Category'];
if(isset($product['Product']['id']) and ($product['Product']['id'])):
$products[$op] = $product;
$op++;
endif;

}
}
if(isset($products) and ($products))
{
$x =0;
foreach($products as $product)
{
$MasterProduct_0 =
$this->MasterProduct->find('all', array('conditions' => 'MasterProduct.product_id='.$product['Product']['id'].' 
										and MasterProduct.seller_id = "'.$Seller_id.'"'));
$MasterProduct = $MasterProduct_0[0];
$products[$x]['Product']['MasterProduct']= $MasterProduct['MasterProduct'];
$stock_detail = $this->_get_stock_position($product['Product']['stock']);
$products[$x]['Product']['stock_detail']= $stock_detail;
$conditions = $this->_get_product_condition($MasterProduct['MasterProduct']['conditions']);
$products[$x]['Product']['Pcondition']= $conditions;
$x++;
}
//pr($products);
//exit;
$this->Set('products', $products);
}

}
}


function get_attributes_cat()
{
echo __METHOD__;
$this->layout ="ajax";
$arribute_groups = $this->AttrGroup->find('all', array('conditions' => 'AttrGroup.cat_id='.$_REQUEST['cat_id'].'',
 'fields' => 'AttrGroup.id'));
if(isset($arribute_groups) and ($arribute_groups))
{
$x=0;
foreach($arribute_groups as $Gp_ID)
{
$GRP_ATTR = $this->AttrGroup->findById($Gp_ID['AttrGroup']['id']);
$Attributes = $this->Attribute->find('all', array(
'conditions' => 'Attribute.attr_gp_id="' .$Gp_ID['AttrGroup']['id']. '"',
 'fields' => 'Attribute.*'
));


$Gp_atr_1[$x]=$GRP_ATTR;
$Gp_atr_1[$x]['AttrGroup']['Attributes'] = $Attributes;
$x++;
}
//pr($Gp_atr_1);
//exit;
$this->Set('Special_Specifications', $Gp_atr_1);
}




}

function admin_get_option_values()
{
echo __METHOD__;
$this->layout = "ajax";
$params = $this->params;
if(isset($params['form']['val']) and ($params['form']['val']>0))
{
$option_values = $this->OptionValue->find('all', array('conditions' => 'OptionValue.option_id='.$params['form']['val'].''));
if(isset($option_values) and ($option_values))
{
foreach($option_values as $option_value)
{
$list[''.$option_value['OptionValue']['id'].''] = $option_value['OptionValue']['value_name'];
}
$this->Set('list', $list);
}
}
}

function get_brands_values()
{
echo __METHOD__;
$this->layout = "ajax";
$params = $this->params;
if(isset($params['form']['cat_id']) and ($params['form']['cat_id']))
{
$cat_id = $params['form']['cat_id'];
$brands = $this->Brand->find('all', array('conditions' => 'Brand.cat_id Like "%'.$cat_id.'%"'));
if(isset($brands) and ($brands))
{
//pr($brands);
//exit;
echo '<select id="ProductBrandId" class="mws-textinput" name="data[Product][brand_id]">';
foreach($brands as $brand)
{
//pr($brand);
//exit;
echo '
										<option value="'.$brand['Brand']['id'].'">'.$brand['Brand']['name'].'</option>
									     ';
}
echo '</select>';
}
else
{
echo '<select id="ProductBrandId" class="mws-textinput" name="data[Product][brand_id]">';
echo '
										<option value="">No Brads</option>
									     ';
echo '</select>';
}
}
exit;
}


function products_search()
{
echo __METHOD__;
if(isset($this->data) and ($this->data))
{
$search_entry = $this->data['Product']['Search'];
$this->paginate['Product'] = array('conditions' => 'Product.name Like "%'.$search_entry.'%" or Product.upc Like "%'.$search_entry.'%"
				or Product.wisn Like "%'.$search_entry.'%" or Product.p_model Like "%'.$search_entry.'%" and Product.list_access = "1"',
 'limit' => 24,
 'fields' => array('Product.*'),
 'order' => 'Product.created Desc'
);

$products = $this->paginate('Product');
$this->Set('products', $products);
}
}

function DisplaySelectedItem($wisn="")
{
echo __METHOD__;
/* 			$params = $this->params;
  if(isset($params) and ($params) and (isset($params['pass'][0]) and $params['pass'][0]))
  {
  $id = base64_decode($params['pass'][0]);
  if(isset($params['named']['access']) and ($params['named']['access']=='noaccessed'))
  {
  $product = $this->MasterProduct->findById($id);
  if(isset($product) and ($product))
  {
  $this->Set('product', $product);
  }
  }
  $this->Set('wisn_n', $params['named']['Wisn']);
  }
  else
  {
  $attrs = $this->_getAttributes('master_products');
  foreach($attrs as $key=>$value)
  {
  $attrsM['MasterProduct'][''.$value.''] = "";
  }
  $this->Set('product', $attrsM);
  }
 */
$params = $this->params;
//pr($params);
//exit;
if(isset($params['pass'][0]) and ($params['pass'][0]))
{

//echo "HIHI";
$id = base64_decode($params['pass'][0]);
if(isset($params['named']['access']) and ($params['named']['access']=='noaccessed'))
{
$product = $this->MasterProduct->findById($id);
$getProduct = $this->Product->findById($product['MasterProduct']['product_id']);

if(isset($getProduct) and ($getProduct))
{
$product['Product'] = $getProduct['Product'];
}
if(isset($product) and ($product))
{
//pr($product);
//exit;	
$this->Set('product', $product);
$showable_products = $this->_getProducts('Product.status="1" and Product.id="'.$product['Product']['id'].'"');
//echo "abobe";
//pr($showable_products);
//exit;	
$this->Set('showable_products', $showable_products);
}
$this->Set('wisn_n', $params['named']['Wisn']);
}
}
else
{
//echo "MMMM";
if(isset($params['named']['Wisn']) and ($params['named']['Wisn']))
{
$this->Set('wisn_n', $params['named']['Wisn']);
$attrs = $this->_getAttributes('master_products');
foreach($attrs as $key => $value)
{
$attrsM['MasterProduct'][''.$value.''] = "";
}
$attrs = $this->Product->findByWisn($params['named']['Wisn']);
foreach($attrs['Product'] as $key => $value)
{
$attrsM['Product'][''.$key.''] = $value;
}
//pr($attrsM);	
$showable = $this->_getProducts('Product.status="1" and Product.id="'.$attrsM['Product']['id'].'"');
if(isset($showable[0]) and ($showable[0])){
$this->Set('paramshowable_products', $showable[0]);
}else{
if(isset($attrsM['Product']['id']) and ($attrsM['Product']['id']) and
isset($attrsM['Product']['status']) and ($attrsM['Product']['status']==1))
{
$showable['Product'] = $attrsM['Product'];
$showable['Product']['MasterProduct'] = $attrsM['MasterProduct'];
}
}
/* if(isset($attrsM['Product']['id']) and ($attrsM['Product']['id']) and 
  isset($attrsM['Product']['status']) and ($attrsM['Product']['status']==1))
  {
  $showable['Product'] = $attrsM['Product'];
  $showable['Product']['MasterProduct'] = $attrsM['MasterProduct'];
  } */
//echo "down";
$this->Set('product', $attrsM);
}
}
$__list_pr_conditions = $this->__list_pr_conditions();
$this->Set('list_pr_conditions', $__list_pr_conditions);

$_list_brands = $this->_list_brands();
$this->Set('_list_brands', $_list_brands);

$_list_departments = $this->_list_departments();
$this->Set('_list_departments', $_list_departments);


$_list_categories = $this->_list_categories();
$cat_keys = array_keys($_list_categories);


$x=0;
foreach($cat_keys as $cat_id)
{
$cat_name = $this->Category->findById($cat_id, array('fields' => 'Category.name'));

$_list_categori[$x]['subcatecategories'] = $this->_get_subcatecategories($cat_id);
if(isset($_list_categori[$x]['subcatecategories']) and ($_list_categori[$x]['subcatecategories']))
{
$_list_categori[$x]['name'] = $cat_name['Category']['name'];
}
$x++;
}
$this->Set('_list_categories', $_list_categori);
$_list_stock_status = $this->_list_stock_status();
$this->Set('list_stock_status', $_list_stock_status);
if(isset($this->data) and ($this->data))
{
//pr($this->data);


$product_id = $this->__get_product_id_from_wisn($this->data['MasterProduct']['product_wisn']);
$product_weight = $this->Product->findById($product_id, array('fields' => 'Product.weight'));
$product_weight['Product']['weight'] = str_replace(' ', '', $product_weight['Product']['weight']);
$product_weight['Product']['weight'] = str_replace('lb', '', $product_weight['Product']['weight']);
$product_weight_exp = explode(' ', $product_weight['Product']['weight']);
//pr($product_weight_exp);
if(isset($product_weight_exp[0]) and ($product_weight_exp[0]))
{
$product_weight = $product_weight_exp[0];
}
else
{
$product_weight= 0;
}
$this->data['MasterProduct']['seller_id'] = $this->Auth->User('Seller_id');
$check_extence = $this->_exiting_seller_offer($this->data['MasterProduct']['seller_id'], $product_id);
if(isset($this->data['MasterProduct']['id']) and ($this->data['MasterProduct']['id']))
{
$this->data['MasterProduct']['product_id'] = $product_id;
$this->data['MasterProduct']['wisn'] = $this->data['MasterProduct']['product_wisn'];
$this->data['MasterProduct']['user_id'] = $this->Auth->User('id');
if(isset($this->data['MasterProduct']) and ($this->data['MasterProduct']))
{
$product_defined_wieght =
$this->_get_seller_shipping_rate($product_weight, $this->data['MasterProduct']['seller_id']);

/* $this->Seller->findById($this->data['MasterProduct']['seller_id'],
  array('fields'=>'Seller.ship_price_w_lb'));
  $product_defined_wieght = $product_defined_wieght['Seller']['ship_price_w_lb']; */
//pr($product_defined_wieght );
//exit;
if(isset($product_defined_wieght) and ($product_defined_wieght))
{
//pr($product_defined_wieght);
//exit;
if(isset($this->data['MasterProduct']['shipping_method']) and
($this->data['MasterProduct']['shipping_method']=='MSF'))
{
$this->data['MasterProduct']['shipping_price'] =
($product_weight*$product_defined_wieght);
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}
//exit;
$this->MasterProduct->Save($this->data['MasterProduct']);
$this->Session->setFlash('Your Offer has been Updated.');
$this->redirect(array('action' => 'inventory'));
}
}else
{
$product_id = $this->__get_product_id_from_wisn($this->data['MasterProduct']['product_wisn']);
$product_weight = $this->Product->findById($product_id, array('fields' => 'Product.weight'));

$product_weight_exp = explode(' ', $product_weight['Product']['weight']);
if(isset($product_weight_exp[0]) and ($product_weight_exp[0]))
{
$product_weight = $product_weight_exp[0];
}
else
{
$product_weight= 0;
}
$check_extence = $this->_exiting_seller_offer($this->data['MasterProduct']['seller_id'], $product_id);
if(isset($check_extence) and ($check_extence))
{
$this->Session->setFlash('Your Already offered for this product.');
$this->redirect(array('action' => 'inventory'));
}
else
{
//echo "You";
//exit;
$this->data['MasterProduct']['product_id'] = $product_id;
$this->data['MasterProduct']['wisn'] = $this->data['MasterProduct']['product_wisn'];
$this->data['MasterProduct']['user_id'] = $this->Auth->User('id');
if(isset($this->data['MasterProduct']) and ($this->data['MasterProduct']))
{
//echo "hi";
//echo $this->data['MasterProduct']['seller_id'];
$product_defined_wieght = /* $this->Seller->findById($this->data['MasterProduct']['seller_id'],
  array('fields'=>'Seller.ship_price_w_lb'));
  $product_defined_wieght = $product_defined_wieght['Seller']['ship_price_w_lb']; */
//pr($product_defined_wieght);
$this->_get_seller_shipping_rate($product_weight, $this->data['MasterProduct']['seller_id']);
//exit;
if(isset($product_defined_wieght) and ($product_defined_wieght))
{
if(isset($this->data['MasterProduct']['shipping_method']) and
($this->data['MasterProduct']['shipping_method']=='MSF'))
{
$this->data['MasterProduct']['shipping_price'] =
($product_weight*$product_defined_wieght);
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}
//pr($this->data['MasterProduct']);
//exit;
//exit;
//pr($this->data['MasterProduct']);
//exit;
$this->MasterProduct->Save($this->data['MasterProduct']);
$this->Session->setFlash('Your Offer has been submitted.');
$this->redirect(array('action' => 'inventory'));
}
}
}
}
//exit;
}

function add_a_product()
{
echo __METHOD__;
$params = $this->params;
//pr($params);
//exit;
if(isset($params['pass'][0]) and ($params['pass'][0]))
{
$id = base64_decode($params['pass'][0]);
//echo $id;
//exit;
if(isset($params['named']['access']) and ($params['named']['access']=='noaccessed'))
{
$product = $this->MasterProduct->findById($id);
$getProduct = $this->Product->findById($product['MasterProduct']['product_id']);

if(isset($getProduct) and ($getProduct))
{
$product['Product'] = $getProduct['Product'];
}
if(isset($product) and ($product))
{
$Gp_atr_1 = $this->_product_specification($product['Product']['id']);
//pr($Gp_atr_1 );
//exit;
$this->Set('Special_Specifications', $Gp_atr_1);
$this->Set('product', $product);
}
}
else
{
$product = $this->MasterProduct->findById($id);
$getProduct = $this->Product->findById($product['MasterProduct']['product_id']);
if(isset($getProduct) and ($getProduct))
{
$product['Product'] = $getProduct['Product'];
}
if(isset($product) and ($product))
{
$Gp_atr_1 = $this->_product_specification($product['Product']['id']);
//pr($Gp_atr_1 );
//exit;
$this->Set('Special_Specifications', $Gp_atr_1);
$this->Set('product', $product);
}
}
}

else
{
$attrs = $this->_getAttributes('master_products');
foreach($attrs as $key => $value)
{
$attrsM['MasterProduct'][''.$value.''] = "";
}
$attrs = $this->_getAttributes('products');
foreach($attrs as $key => $value)
{
$attrsM['Product'][''.$value.''] = "";
}
$Gp_atr_1 = $this->_product_specification($attrsM['Product']['id']);
$this->Set('Special_Specifications', $Gp_atr_1);
$this->Set('product', $attrsM);
}

$__list_options = $this->__list_options();
$this->Set('list_options', $__list_options);
$_list_categories = $this->_list_categories();
$cat_keys = array_keys($_list_categories);

$x=0;
foreach($cat_keys as $cat_id)
{
$cat_name = $this->Category->findById($cat_id, array('fields' => 'Category.name'));

$_list_categori[$x]['subcatecategories'] = $this->_get_subcatecategories($cat_id);
if(isset($_list_categori[$x]['subcatecategories']) and ($_list_categori[$x]['subcatecategories']))
{
$_list_categori[$x]['name'] = $cat_name['Category']['name'];
}
$x++;
}
$this->Set('_list_categories', $_list_categori);

$_list_brands = $this->_list_brands();


$this->Set('_list_brands', $_list_brands);
$_list_departments = $this->_list_departments();
$this->Set('_list_departments', $_list_departments);

$attrGroups = $this->__List_Group_attributes('');
$this->Set('attrGroups', $attrGroups);
$_list_attributes = $this->_list_attributes('', 3);
$this->Set('_list_attributes', $_list_attributes);

$_list_stock_status = $this->_list_stock_status();
$this->Set('list_stock_status', $_list_stock_status);

$__list_pr_conditions = $this->__list_pr_conditions();
$this->Set('list_pr_conditions', $__list_pr_conditions);

if(isset($this->data) and ($this->data))
{
if(isset($this->data['Product']) and ($this->data['Product']) and ($this->data['MasterProduct']['price']))
{

if(isset($this->data['Brand']['name']) and ($this->data['Brand']['name']))
{
$subcats = $this->_get_subcatecategories($this->data['Product']['cat_id'], '');
if(isset($subcats) and ($subcats))
{
foreach($subcats as $subcat)
{
$subcat_id[] = $subcat['Category']['id'];
}
}
$subcat_id[] = $this->data['Product']['cat_id'];
$imp_subcats = implode(',', $subcat_id);
$this->data['Brand']['cat_id'] = $imp_subcats;
$slug_string = str_replace('', '-', $this->data['Brand']['name']);
$this->data['Brand']['slug'] = $this->Brand->generateSlug($slug_string);
$this->Brand->save($this->data['Brand']);
$brand_id = $this->Brand->getLastInsertId();
if(isset($brand_id) and $brand_id)
{
$this->data['Product']['brand_id']= $brand_id;
}
}
$parent_cat = $this->__get_parent_id($this->data['Product']['cat_id'], "Category");
$this->data['Product']['parent_cat_id'] = $parent_cat;
$slug = str_replace(" ", "_", $this->data['Product']['name']);
$slug = strtolower($slug);
$this->data['Product']['slug'] = $this->Product->generateSlug($slug);
$this->data['Product']['hash'] = $this->__generateKey('Product');
$this->data['Product']['wisn'] = $this->__generateWisn('Product');
$this->data['Product']['created_by'] = $this->Auth->User('id');
$this->data['Product']['weight'] = $this->data['Product']['weight'].' '.$this->data['Product']['weight_unit'];
$this->data['Product']['fullfiled_by'] = "Merchant";
$this->Product->Save($this->data['Product']);
if(isset($this->data['Product']['id']) and ($this->data['Product']['id']))
{
$last_id = $this->data['Product']['id'];

}else
{
$last_id = $this->Product->getLastInsertId();
}

if(isset($this->data['MasterProduct']) and ($this->data['MasterProduct']))
{
if(isset($this->data['MasterProduct']['qty']) and ($this->data['MasterProduct']['qty']))
{
$this->data['MasterProduct']['qty'] = $this->data['MasterProduct']['qty'];
}else{
$this->data['MasterProduct']['qty'] = 1;
}

$this->data['MasterProduct']['user_id'] = $this->Auth->User('id');
$this->data['MasterProduct']['seller_id'] = $this->Auth->User('Seller_id');
$this->data['MasterProduct']['product_id'] = $last_id;
$this->data['MasterProduct']['wisn'] = $this->data['Product']['wisn'];
$product_defined_wieght = $this->_get_seller_shipping_rate($this->data['Product']['weight'], $this->data['MasterProduct']['seller_id']); /* $this->Seller->findById($this->data['MasterProduct']['seller_id'],
  array('fields'=>'Seller.ship_price_w_lb'));
  $product_defined_wieght = $product_defined_wieght['Seller']['ship_price_w_lb']; */
if(isset($product_defined_wieght) and ($product_defined_wieght))
{
if(isset($this->data['MasterProduct']['shipping_method']) and
($this->data['MasterProduct']['shipping_method']=='MSF'))
{
$this->data['MasterProduct']['shipping_price'] =
($this->data['Product']['weight']*$product_defined_wieght);
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}

$this->MasterProduct->Save($this->data['MasterProduct']);
}

if(isset($this->data['Product']['Attr']) and ($this->data['Product']['Attr']))
{
foreach($this->data['Product']['Attr'] as $attr)
{
if(isset($attr['attr_value']) and ($attr['attr_value']))
{
if(isset($attr['id']) and($attr['id'])){
$this->data['Specification']['id'] = $attr['id'];
}else{
$this->data['Specification']['id'] = "";
}
$this->data['Specification']['key'] = $attr['key'];
$this->data['Specification']['attr_id'] = $attr['attribute'];
$this->data['Specification']['value'] = $attr['attr_value'];
$this->data['Specification']['product_id'] = $last_id;
$this->Specification->Save($this->data['Specification']);
}
}
}
if(isset($this->data['Product']['multi']) and
($this->data['Product']['multi'][0]['name'])
and sizeof($this->data['Product']['multi'][0]['name']))
{
$this->data['Product']['id'] = $last_id;
$xo =1;
foreach($this->data['Product']['multi'] as $multiupload)
{
if(isset($multiupload) and ($multiupload) and ($multiupload['name']))
{
$image = $multiupload;
$name = str_replace(' ', '_', $image['name']);
$nameex_explode = explode('.', $name);
$nameex = $nameex_explode[0];
$name_ext = $nameex_explode[1];
$filtered = $last_id.''.($xo*(date('ymd').time())).".".$name_ext;
$image['name'] = strtolower($filtered);
$combined_name[] = $image['name'];
$upload_file = $this->upload_img($last_id, $image, 'Product');
$xo++;
}
}
$this->data['Product']['rel_img'] = implode(', ', $combined_name);
$this->Product->save($this->data['Product']);
}
if(isset($this->data['Product']['imgImage']) and ($this->data['Product']['imgImage'])
and ($this->data['Product']['imgImage']['name']))
{
$image = $this->data['Product']['imgImage'];
$this->data['Product']['id'] = $last_id;
$Job_image_name = str_replace(' ', '_', $image['name']);
$job_image_name_explode = explode('.', $Job_image_name);
$job_image_name_explode_start_name = $job_image_name_explode[0];
$job_image_name_explode_ext = $job_image_name_explode[1];
$job_image_name_filtered = date('ymd').time().$last_id.".".$job_image_name_explode_ext;
$this->data['Product']['img'] = strtolower($job_image_name_filtered);
$image['name'] = strtolower($job_image_name_filtered);
;
$upload_file = $this->upload_img("", $image, 'Product');
$this->Product->save($this->data['Product']);
}

if(isset($this->data['Product']['option']) and ($this->data['Product']['option']['option_str']))
{
$option_id = $this->Option->findBySlug($this->data['Product']['option']['option_str']);
$option_id = $option_id['Option']['id'];
$x=0;
foreach($this->data['Product']['option'] as $product_option)
{
if(isset($x) and ($x==0))
{
}
else
{
$this->data['OptionValue']['id'] = "";
$this->data['OptionValue']['option_id'] = $option_id;
$this->data['OptionValue']['value_name'] = $product_option['value_name'];
//pr($product_option['option_value']); 
//exit;
$option_value = implode('-', $product_option['option_value']);
//pr($option_value);
//exit;
$this->data['OptionValue']['option_value'] = $option_value;
if(isset($product_option['qty']) and ($product_option['qty']))
{
$this->data['OptionValue']['qty'] = $product_option['qty'];
}
else
{
$this->data['OptionValue']['qty'] = 1;
}
$this->data['OptionValue']['subtract_stock'] = $product_option['subtract_stock'];
if(isset($product_option['price']) and ($product_option['price']))
{
$this->data['OptionValue']['price'] = $product_option['price'];
}
else
{
$this->data['OptionValue']['price'] ="";
}
$this->data['OptionValue']['product_id'] = $last_id;
$this->OptionValue->Save($this->data['OptionValue']);
}
$x++;
}
}

$this->redirect(array('action' => 'inventory'));
}
else
{
$this->Session->setFlash('Please enter a correct price ammount');
$this->render('add_a_product');
}
}
}

function get_attributes_values()
{
echo __METHOD__;
$this->layout = "ajax";
$params = $this->params;
if(isset($params['form']['val']) and ($params['form']['val']) and ($params['form']['val']!='undefined'))
{
$subdivisions = $this->Attribute->find("all",
 array(
'conditions' => 'Attribute.attr_gp_id="'.$params['form']['val'].'"',
 'fields' => array('Attribute.id', 'Attribute.name', 'Attribute.required'
), 'limit' => ""));

//pr($subdivisions);
//exit;	
$this->Set('_list_attributes', $subdivisions);
}
else
{
$this->Set('message', 'Please select other Attribute Group');
}

}

function get_option_results()
{
echo __METHOD__;
$this->layout = "ajax";
$paramsoption = $this->params['form']['option'];
if(isset($paramsoption) and ($paramsoption))
{
$this->set('options', $paramsoption );
}
}

function get_brand_lock()
{
echo __METHOD__;
$this->layout = "ajax";
$paramsoption = $this->params['form']['brand_name'];
if(isset($paramsoption) and ($paramsoption))
{
$exist_brand = $this->Brand->find('first', array('conditions' => 'Brand.name Like "%'.$paramsoption.'%" or Brand.suggested_name Like "%'.$paramsoption.'%"'));
if(isset($exist_brand) and ($exist_brand))
{
$this->set('brand', $exist_brand['Brand']['name']);
}
}
}

function InfoAndPolicies()
{
echo __METHOD__;
}
function seller_aboutus($type="")
{
echo __METHOD__;
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$about_us = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$seller_token.'" and SellerPage.type="aboutus"'));
$this->Set('seller_about_us', $about_us);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['SellerPage']) and ($this->data['SellerPage']))
{
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$this->data['SellerPage']['seller_id'] = $seller_id;
$this->data['SellerPage']['seller_token'] = $seller_token;
$this->data['SellerPage']['title'] = "About Us";
$this->SellerPage->Save($this->data['SellerPage']);
$this->redirect(array('action' => 'InfoAndPolicies'));
}
}
}


function seller_shipping($type="")
{
echo __METHOD__;
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$about_us = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$seller_token.'" and SellerPage.type="seller_shipping"'));
$this->Set('seller_about_us', $about_us);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['SellerPage']) and ($this->data['SellerPage']))
{
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$this->data['SellerPage']['seller_id'] = $seller_id;
$this->data['SellerPage']['seller_token'] = $seller_token;
$this->data['SellerPage']['title'] = "Seller Shipping";
$this->SellerPage->Save($this->data['SellerPage']);
$this->redirect(array('action' => 'InfoAndPolicies'));
}
}
}

function seller_privacy_policy($type="")
{
echo __METHOD__;
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$about_us = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$seller_token.'" and SellerPage.type="seller_privacy_policy"'));
$this->Set('seller_about_us', $about_us);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['SellerPage']) and ($this->data['SellerPage']))
{
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$this->data['SellerPage']['seller_id'] = $seller_id;
$this->data['SellerPage']['seller_token'] = $seller_token;
$this->data['SellerPage']['title'] = "Privacy Policy";
$this->SellerPage->Save($this->data['SellerPage']);
$this->redirect(array('action' => 'InfoAndPolicies'));
}
}
}

function seller_faq($type="")
{
echo __METHOD__;
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$about_us = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$seller_token.'" and SellerPage.type="seller_faq"'));
$this->Set('seller_about_us', $about_us);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['SellerPage']) and ($this->data['SellerPage']))
{
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$this->data['SellerPage']['seller_id'] = $seller_id;
$this->data['SellerPage']['seller_token'] = $seller_token;
$this->data['SellerPage']['title'] = "FAQ";
$this->SellerPage->Save($this->data['SellerPage']);
$this->redirect(array('action' => 'InfoAndPolicies'));
}
}
}


function seller_gift_services($type="")
{
echo __METHOD__;
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$about_us = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$seller_token.'" and SellerPage.type="seller_gift_services"'));
$this->Set('seller_about_us', $about_us);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['SellerPage']) and ($this->data['SellerPage']))
{
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$this->data['SellerPage']['seller_id'] = $seller_id;
$this->data['SellerPage']['seller_token'] = $seller_token;
$this->data['SellerPage']['title'] = "Gift Services";
$this->SellerPage->Save($this->data['SellerPage']);
$this->redirect(array('action' => 'InfoAndPolicies'));
}
}
}

function seller_logo()
{
echo __METHOD__;
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$this->Set('seller_details', $seller_details);
if(isset($this->data) and ($this->data))
{
$seller_id = $this->Auth->User('Seller_id');
if(isset($this->data['Seller']['logoImg']) and ($this->data['Seller']['logoImg'])
and ($this->data['Seller']['logoImg']['name']))
{
$last_id = $seller_id;
$image = $this->data['Seller']['logoImg'];
$this->data['Seller']['id'] = $last_id;
$Job_image_name = str_replace(' ', '_', $image['name']);
$job_image_name_explode = explode('.', $Job_image_name);
$job_image_name_explode_start_name = $job_image_name_explode[0];
$job_image_name_explode_ext = $job_image_name_explode[1];
$job_image_name_filtered = $last_id.".".$job_image_name_explode_ext;
$this->data['Seller']['logo'] = strtolower($job_image_name_filtered);
$image['name'] = strtolower($job_image_name_filtered);
$upload_file = $this->upload_img($last_id, $image, 'Seller');
$this->Seller->save($this->data['Seller']);
}
}
}



function seller_pages($type="")
{
echo __METHOD__;
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$about_us = $this->SellerPage->find('all',
 array('conditions' => 'SellerPage.seller_token="'.$seller_token.'" and SellerPage.type="custom"'));
$this->Set('seller_about_us', $about_us);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['SellerPage']) and ($this->data['SellerPage']))
{
$seller_id = $this->Auth->User('Seller_id');
$seller_details = $this->__get_details($seller_id, "Seller");
$seller_token = $seller_details['Seller']['merchant_token'];
$this->data['SellerPage']['seller_id'] = $seller_id;
$this->data['SellerPage']['seller_token'] = $seller_token;
$this->SellerPage->Save($this->data['SellerPage']);
$last_id = $this->SellerPage->getLastInsertId();

if(isset($this->data['SellerPage']['logoImg']) and ($this->data['SellerPage']['logoImg'])
and ($this->data['SellerPage']['logoImg']['name']))
{
$image = $this->data['SellerPage']['logoImg'];
$this->data['SellerPage']['id'] = $last_id;
$Job_image_name = str_replace(' ', '_', $image['name']);
$job_image_name_explode = explode('.', $Job_image_name);
$job_image_name_explode_start_name = $job_image_name_explode[0];
$job_image_name_explode_ext = $job_image_name_explode[1];
$job_image_name_filtered = $last_id.".".$job_image_name_explode_ext;
$this->data['SellerPage']['img'] = strtolower($job_image_name_filtered);
$image['name'] = strtolower($job_image_name_filtered);
$upload_file = $this->upload_img($last_id, $image, 'SellerPage');
$this->SellerPage->save($this->data['SellerPage']);
}
$this->redirect(array('action' => 'InfoAndPolicies'));
}
}
}

function seller_accountinfo()
{
echo __METHOD__;
$tries = $this->__list_countries();
$this->Set('list_countries', $tries);
$user_id = $this->Auth->User('id');

$seller_id = $this->Auth->User('Seller_id');
//pr($seller_id );
//exit;

$_list_shipping_service_providers = $this->_list_shipping_service_providers();
$this->Set('list_shipping_service_providers', $_list_shipping_service_providers);

$seller_info = $this->Seller->findById($seller_id);
if(isset($seller_info['Seller']['ship_services']) and ($seller_info['Seller']['ship_services']))
{
$shipping_services = explode(',', $seller_info['Seller']['ship_services']);
$x=0;
foreach($shipping_services as $ssp)
{
$ssp_array[] = $ssp;
$service_name = $this->ShippingSp->findById($ssp, array('fields' => 'ShippingSp.name'));
$service_nam[] = $service_name['ShippingSp']['name'];
$x++;
}
$seller_info['Seller']['ship_services']= $ssp_array;
if(isset($service_nam) and ($service_nam))
{
$seller_info['Seller']['ship_services_exp'] = $service_nam;
}
}


$active_adress = $this->Address->find('all',
 array('conditions' => 'Address.active="1" and Address.user_id ='.$user_id.''));
//pr($active_adress);
//exit;
if(isset($active_adress[0]) and ($active_adress[0]))
{
$seller_info['address'] = $active_adress[0]['Address'];
}
if(isset($seller_info) and ($seller_info))
{
//pr($seller_info);
//exit;
$this->Set('seller_info', $seller_info);
}
$Paymentinfo = $this->Paypal->find('first', array('conditions' => 'Paypal.user_id='.$user_id.''));
$this->Set('Paymentinfo', $Paymentinfo);


if(isset($this->data) and ($this->data))
{
//pr($this->data);
//exit;

if(isset($this->data['Seller']) and ($this->data['Seller']))
{
if(isset($this->data['Seller']['ship_services']) and ($this->data['Seller']['ship_services']))
{
$this->data['Seller']['ship_services'] = implode(',', $this->data['Seller']['ship_services']);
}
if(isset($this->data['Seller']['shipping']) and ($this->data['Seller']['shipping']))
{
foreach($this->data['Seller']['shipping'] as $shipps)
{
//pr($shipps);
//exit;
$hipps_1[] = $shipps['ship_price_from'].'-'.$shipps['ship_price_to'].'_'.$shipps['ship_price_w_lb'];
}
if(isset($hipps_1) and ($hipps_1))
{

$hipps_1_imp = implode(', ', $hipps_1);
$this->data['Seller']['ship_price_w_lb'] = $hipps_1_imp;
}
}
//pr($this->data['Seller']);
//exit;
$this->Seller->Save($this->data['Seller']);
$this->Session->setFlash('Account Setting  has been updated.');
}
if(isset($this->data['Address']) and ($this->data['Address']))
{
$this->Address->Save($this->data['Address']);
$this->Session->setFlash('Address Setting  has been updated.');
}
$this->redirect(array('action' => 'seller_accountinfo'));
}
}

function logout()
{
echo __METHOD__;
$date=date('Y-m-d h:i:s');
$this->User->updateAll(array('User.last_login' => "'".$date."'"), array('User.id' => $userid));
$this->Session->destroy('user');
$this->Session->setFlash('You\'ve successfully logged out.');
}

function login()
{
echo __METHOD__;
$this->layout = "default";

$this->Auth->login($this->data);

$is_seller = $this->Auth->User('is_seller');

$user_id = $this->Auth->User('id');

if(isset($is_seller))
{
//pr($is_seller);
//exit;
if(isset($is_seller) and ($is_seller==1))
{
$get_seller_id = $this->_get_seller_id($user_id);
//pr($get_seller_id);
//exit;
$this->Session->write('Auth.User.Seller_id', $get_seller_id);
$this->redirect(array('controller' => 'Sellers', 'action' => 'dashboard'));
}
else
{
unset($this->data['User']['username']);
unset($this->data['User']['password']);
/* $this->data['User']['id'] = $this->Auth->User('id'); 
  $this->data['User']['is_seller'] = 1;
  $this->User->Save($this->data['User']); */
/* Here we are activate user account as Seller. */
$this->data['User']['id'] = $this->Auth->User('id');
$this->data['User']['is_seller'] = 1;
$this->User->Save($this->data['User']);
$this->Session->write('Auth.User.is_seller', 1);
/* End here */

$this->redirect(array('controller' => 'Sellers', 'action' => 'account_info'));
}
}
}

function ajax_login()
{
echo __METHOD__;
$this->layout = "ajax";
$params = $this->params;
$this->data['User']['username']=$params['form']['selleremail'];
$this->data['User']['password']=$params['form']['sellerpassword'];
$this->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
$this->Auth->login($this->data);
$is_seller = $this->Auth->User('is_seller');
$user_id = $this->Auth->User('id');
if(isset($user_id) and ($user_id))
{
$get_seller_id = $this->_get_seller_id($user_id);
$this->Session->write('Auth.User.Seller_id', $get_seller_id);
$this->set('login', '1');
}
else
{
exit;
}

}


function login_setting()
{
echo __METHOD__;
$session_user_id = $this->Auth->User('id');
$seller_detail = $this->User->findById($session_user_id);
$this->Set('seller_detail', $seller_detail);
if(empty($this->data)== false)
{
//pr($this->data);
//exit;
$id = $this->Session->read('Auth.User.id');
$user=$this->User->find('first', array('conditions' => array('id' => $id)));
if (!empty($this->data['User']))
{
$userregistered = $this->User->find('first', array('conditions' => 'User.email = "'.$this->data['User']['email'].'"',
 'fields' => 'User.id'));
if(sizeof($userregistered) and ($userregistered))
{
$this->Session->setFlash('Email already in use');
$this->redirect(array('action' => 'login_setting'));
}
if (isset($this->data['User']['passwordenter']) and ($this->data['User']['passwordenter']))
{
//pr();
if( $user['User']['password'] == $this->Auth->password($this->data['User']['passwordenter']))
{
if($this->data['User']['passwordn']==$this->data['User']['password_retype'])
{
$this->data['User']['password']=$this->Auth->password($this->data['User']['passwordn']);
}
else
{
$this->Session->setFlash('Typed passwords did not match.');
$this->redirect(array('action' => 'login_setting'));
}
}
else
{
$this->Session->setFlash('Existing Password is not correct.');
$this->redirect(array('action' => 'login_setting'));
}
}
$this->User->save($this->data['User']);
$this->Session->setFlash('Login Setting has been changed.');
$this->redirect(array('action' => 'login_setting'));
}
}

}

function seller_Mails()
{
echo __METHOD__;
$user_id = $this->Auth->User('id');

$Sent_messages = $this->__Seller_Sentmail($user_id, "", "");
//pr($Sent_messages );
//exit;
$this->Set('messages', $Sent_messages);


$__Seller_Recievedmail = $this->__Seller_Recievedmail($user_id);
//pr($__Seller_Recievedmail);
//exit;

$this->Set('Seller_Recievedmail', $__Seller_Recievedmail);

if(isset($this->data) and ($this->data))
{
if(isset($this->data['Message']['Sent']) and ($this->data['Message']['Sent']))
{
$Sentcount = explode('_', $this->data['Message']['Sent']);
$date = $this->_last_dates($Sentcount[0], $Sentcount[1]);
$Sent_messages = $this->__Seller_Sentmail($user_id, $date, date('Y-m-d'));
$this->Set('messages', $Sent_messages);
}

if(isset($this->data['Message']['Recieved']) and ($this->data['Message']['Recieved']))
{
$Sentcount = explode('_', $this->data['Message']['Recieved']);
$date = $this->_last_dates($Sentcount[0], $Sentcount[1]);
$__Seller_Recievedmail = $this->__Seller_Recievedmail($user_id, $date, date('Y-m-d'));
$this->Set('Seller_Recievedmail', $__Seller_Recievedmail);
}
}

}

function seller_do_mail($buyerhash="", $order_id="")
{
echo __METHOD__;
$Mail_subjects = $this->__list_message_subjects('');
$this->Set('subjects', $Mail_subjects);
$this->Set('order_id', $order_id);
if(isset($buyerhash) and ($buyerhash))
{
$Userdetails = $this->User->find('first', array('conditions' => 'User.hash="'.$buyerhash.'" and User.role = "customer"'));
if(isset($Userdetails) and ($Userdetails))
{
$this->Set('Userdetails', $Userdetails);
}
else
{
$this->redirect(array('action' => 'seller_Mails'));
}
}
if(isset($this->data) and ($this->data))
{
if(isset($this->data['Message']) and ($this->data['Message']))
{
$seller_id = $this->Auth->User('Seller_id');
$seller = $this->Seller->findById($seller_id);
$this->data['Message']['hash'] = $this->__generateKey('Message');
$get_subject = $this->MessageSubject->findById($this->data['Message']['subject_id']);
$this->data['Message']['to_email'] = $this->data['Message']['email'];
$exp_hash = explode('@', $this->data['Message']['email']);
$user_id = $this->User->findByHash($exp_hash[0], array('fields' => 'User.*'));
$this->data['Message']['to_user_id'] = $user_id['User']['id'];
$this->data['Message']['from_email'] =$seller['Seller']['merchant_token'].wondere_email_domain;
$this->data['Message']['from_user_id'] =$this->Auth->User('id');
$this->Message->save($this->data['Message']);
$last_id = $this->Message->getLastInsertId();
if(isset($this->data['Message']['attachment']) and ($this->data['Message']['attachment'])
and ($this->data['Message']['attachment']['name']))
{
$image = $this->data['Message']['attachment'];
$this->data['Message']['id'] = $last_id;
$Job_image_name = str_replace(' ', '_', $image['name']);
$job_image_name_explode = explode('.', $Job_image_name);
$job_image_name_explode_start_name = $job_image_name_explode[0];
$job_image_name_explode_ext = $job_image_name_explode[1];
$job_image_name_filtered = date('ymd').time().".".$job_image_name_explode_ext;
$this->data['Message']['attach'] = strtolower($job_image_name_filtered);
$image['name'] = $this->data['Message']['attach'];
$upload_file = $this->upload_img($last_id, $image, 'MessageAttchment');
$this->Message->save($this->data['Message']);
}
$direc = getcwd();
if(isset($this->data['Message']['attach']) and ($this->data['Message']['attach'])){
$attch = $direc.'/img'.'/MessageAttchment/'.$last_id.'/'.$this->data['Message']['attach'];
}else
{
$attch = "";
}
$mail_persons[$user_id['User']['email']]=$user_id['User']['name'];
$this->__send_mail($mail_persons, $get_subject['MessageSubject']['name'], $this->data['Message']['dec'],
 $this->data['Message']['from_email'], $seller['Seller']['bs_dis_name'], $attch);
$this->redirect(array('action' => 'index'));
}
}

}

function view_mail($mail_hash)
{
echo __METHOD__;
if(isset($mail_hash) and ($mail_hash)){
$message = $this->Message->findByHash($mail_hash);
//pr($message);
$user_id = $this->Auth->User('id');
if(($message['Message']['from_user_id']==$user_id) or ($message['Message']['to_user_id']==$user_id))
{
$subject = $this->MessageSubject->findById($message['Message']['subject_id']);
$message['Message']['SubjectInfo'] = $subject['MessageSubject']['name'];
$this->Set('message', $message);
}
else
{
//$this->Session->setFlash('You are not authorized to this Message');
$this->redirect('/sellers/dashboard');
}
//exit;
}else
{
$this->Session->setFlash('Data is insufficent to view this mail');
$this->redirect('/sellers/dashboard');
}
}

function manage_orders()
{
echo __METHOD__;
$seller_id = $this->Session->read('Auth.User.Seller_id');
$conditions = "OrderItem.seller_id = '".$seller_id."' and OrderItem.payment_status='1'";
$Order_list = $this->_get_seller_orders($conditions);
//pr($Order_list);
//exit;
$this->Set('Order_list', $Order_list);
$UnshippedOrders = $this->_unshipped_seller_orders($seller_id);
if(isset($UnshippedOrders) and ($UnshippedOrders))
{$UnshippedOrdersCount = count($UnshippedOrders);
}
else{ $UnshippedOrdersCount = 0;
}
$this->Set('UnshippedOrdersCount', $UnshippedOrdersCount);
if(isset($this->data) and ($this->data))
{
if(isset($this->data['Orders']['Filter']) and ($this->data['Orders']['Filter']))
{
$Sentcount = explode('_', $this->data['Orders']['Filter']);
$date = $this->_last_dates($Sentcount[0], $Sentcount[1]);
$start_date = $date;
$end_date = date('Y-m-d');
$seller_id = $this->Session->read('Auth.User.Seller_id');
if((isset($start_date) and ($start_date)) and (isset($end_date) and ($end_date)))
{
$condition = 'and created >= "'.$start_date.'" and created <= "'.$end_date.'"';
$conditions = "OrderItem.seller_id = '".$seller_id."' and OrderItem.payment_status='1' ".$condition;
$Order_list = $this->_get_seller_orders($conditions);
$this->Set('Order_list', $Order_list);
}
}
}
}

function manage_finances()
{
echo __METHOD__;
$seller_id = $this->Session->read('Auth.User.Seller_id');
$conditions = "OrderItem.seller_id = '".$seller_id."' and OrderItem.payment_status='1'";
$Order_list = $this->_get_seller_orders($conditions);
$this->Set('finaces_details', $Order_list);
}

function edit_shipment()
{
echo __METHOD__;
$params = $this->params;
if(isset($params['named']['order_item']) and ($params['named']['order_item']))
{
$order_item_id = $params['named']['order_item'];
$order_id = $params['named']['order_id'];
$order_item = $this->OrderItem->findByOrderItemId($order_item_id);
$product = $this->_getProducts('Product.id='.$order_item['OrderItem']['product_id'].'');

$order_item['Product'] = $product[0]['Product'];
$order = $this->Order->findByTrackingId($order_id);
$country = $this->Country->findById($order['Address']['country']);
$order['Address']['country'] = $country['Country']['country_name'];
$order_item['OrderDetail'] = $order;
//pr($order_item);
//exit;
$this->Set('order_item', $order_item);
$seller_id = $this->Auth->User('Seller_id');
$shipping_services = $this->Seller->findById($seller_id, array('fields' => 'Seller.ship_services'));

$exp_shipping_services = explode(",", $shipping_services['Seller']['ship_services']);
//pr($exp_shipping_services);
foreach($exp_shipping_services as $exp_shipping_service)
{
$shipping_sps = $this->ShippingSp->findById($exp_shipping_service);
if(isset($shipping_sps['ShippingSp']['name']) and ($shipping_sps['ShippingSp']['name']))
{
$shipping_sps_array[]= $shipping_sps['ShippingSp']['name'];
}
}
if(isset($shipping_sps_array) and ($shipping_sps_array))
{
$this->Set('shipping_sps_array', $shipping_sps_array);
}/* Start hiding from IE Mac \ */
/* Stop hiding from IE Mac */

}
if(isset($this->data) and ($this->data))
{
//pr($this->data);
//exit;
if(isset($this->data['OrderItem']) and ($this->data['OrderItem']))
{
$this->data['OrderItem']['shipped'] = 1;
$this->OrderItem->save($this->data['OrderItem']);
$this->_mail_to_customer_for_shippment($this->data);

$this->redirect(array('action' => 'manage_orders'));

}
}
//exit;
}

function seller_order_detail($order_id="", $order_item_id="")
{
echo __METHOD__;
$this->Set('order_id', $order_id);
$order_item = $this->OrderItem->findByOrderItemId($order_item_id);
$product = $this->_getProducts('Product.id='.$order_item['OrderItem']['product_id'].'');

$order_item['Product'] = $product[0]['Product'];
$order = $this->Order->findByTrackingId($order_id);
$country = $this->Country->findById($order['Address']['country']);
$order['Address']['country'] = $country['Country']['country_name'];
$order_item['OrderDetail'] = $order;
//pr($order_item);
//exit; 
$this->Set('order_item', $order_item);
//pr($order_item);
//exit;

}

function export($seller_id="")
{
echo __METHOD__;
// SET LAYOUT
$this->layout = 'ajax';
// GRAB DATA
$csv = NULL;
// CSV HEADERS
$csv .= 'Product ID';
$csv .= ', Product Name';
$csv .= ', Product WISN/ISBN';
$csv .= ', Product Qty';
$csv .= ', Product SKU';
$csv .= ', Product created';
$csv .= "\n";

$csv = substr($csv, 0, -1)."\n";
// FEED IN DATA
if(isset($seller_id) and ($seller_id))
{
$seller_id = base64_decode($seller_id);
$conditions = 'MasterProduct.seller_id="'.$seller_id.'"';
}else{
$conditions = "";
}
$merchants = $this->MasterProduct->find('all', array('fields' => 'DISTINCT MasterProduct.product_id', 'conditions' => ''.$conditions.''));
//pr($merchants);
//exit;
$x=0;
if(isset($merchants) and ($merchants))
{
foreach($merchants as $merchant)
{
$merchants = $this->MasterProduct->findByProductId($merchant['MasterProduct']['product_id']);

$results = $this->Product->findById($merchant['MasterProduct']['product_id'], array('fields' => 'Product.id, Product.name, Product.wisn'));
if(isset($results) and ($results))
{
$MuM[$x]['MasterProduct'] = $merchants['MasterProduct'];
$MuM[$x]['Product'] = $results['Product'];
$x++;
}

}
if(isset($MuM) and ($MuM))
{
foreach($MuM as $result)
{
$csv .= $result['Product']['id'];
$csv .= ', '.$result['Product']['name'];
$csv .= ', '.$result['Product']['wisn'];
$csv .= ', '.$result['MasterProduct']['qty'];
$csv .= ', '.$result['MasterProduct']['sku'];
$csv .= ', '.$result['MasterProduct']['created'];
$csv .= "\n";
}
}
else
{
$csv .= "Currently No Products History Available.";
$csv .= "\n";
}
}
else
{
$csv .= ", Currently No Products History Available.";
$csv .= "\n";
}

// CREATE FILE AND INSTANTLY DOWNLOAD
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv; filename=all_products".date("Y-m-d").".csv; size=".strlen($csv));
echo $csv;
exit;
}

/* 				function export($seller_id="")
  {
  $seller_id = base64_decode($seller_id);
  $this->layout = "ajax";
  ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large
  $filename = "export_".date("Y.m.d").".csv";
  $csv_file = fopen('php://output', 'w');
  header('Content-type: application/csv');
  header('Content-Disposition: attachment; filename="'.$filename.'"');
  if(isset($seller_id) and ($seller_id))
  {
  $conditions = 'MasterProduct.seller_id="'.$seller_id.'"';
  }else{
  $conditions = "";
  }
  $merchants = $this->MasterProduct->find('all', array('fields'=>'DISTINCT MasterProduct.product_id', 'conditions'=>''.$conditions.''));
  $x=0;
  foreach($merchants as $merchant)
  {
  $merchants = $this->MasterProduct->findByProductId($merchant['MasterProduct']['product_id']);

  $results = $this->Product->findById($merchant['MasterProduct']['product_id'], array('fields'=>'Product.id, Product.name, Product.wisn'));
  if(isset($results) and ($results))
  {
  $MuM[$x]['MasterProduct'] = $merchants['MasterProduct'];
  $MuM[$x]['Product'] = $results['Product'];
  $x++;
  }

  }
  $products_attributes = $this->_getAttributes('products');
  $products_attributes = implode(", ",$products_attributes);
  $master_products = $this->_getAttributes('master_products');
  $master_products = implode(", ",$master_products);
  $header_row = array("ID", "Product_Name", "wisn", "qty", "sku","Created");
  fputcsv($csv_file,$header_row,',','"');
  foreach($MuM as $result)
  {
  $row = array(
  $result['Product']['id'],
  $result['Product']['name'],
  $result['Product']['wisn'],
  $result['MasterProduct']['qty'],
  $result['MasterProduct']['sku'],
  $result['MasterProduct']['created'],
  );
  fputcsv($csv_file,$row,',','"');
  }
  fclose($csv_file);
  exit;
  } */

function uploadcsv()
{
echo __METHOD__;
$current_directory = getcwd();
require_once($current_directory.'/../vendors/parsecsv/parsecsv.lib.php');
$csv = new parseCSV();
if(isset($_FILES['uploadFileName']) and ($_FILES['uploadFileName']))
{
$csv->auto($_FILES['uploadFileName']['tmp_name']);
$data = $csv->data;
//pr($data);
//exit;
$c=0;
foreach($data as $mydata)
{
$x=0;
foreach($mydata as $key => $value)
{
$key = explode('-', $key);
$key = $key[0];
//echo $key."<br>";
$keyLetter = explode(':', $key);
$keyLetterKey = $keyLetter[0];
if(isset($keyLetter[1]) and ($keyLetter[1]) )
{

if($keyLetterKey=='M')
{

$keyname = str_replace(" ", "", $keyLetter[1]);
if(isset($keyname) and ($keyname=='price'))
{
$Product[$c]['MasterProduct'][''.$keyname.''] = str_replace(",", "", $value);
}
else{
$Product[$c]['MasterProduct'][''.$keyname.''] = $value;
}
}
else
{
$keyname = str_replace(" ", "", $keyLetter[1]);
if(isset($keyname) and ($keyname=='cat_id'))
{
$cat_id = $this->Category->findByName($value, array('fields' => 'Category.id'));
if(isset($cat_id) and ($cat_id)):
$Product[$c]['Product'][''.$keyname.''] = $cat_id['Category']['id'];
endif;
}
elseif(isset($keyname) and ($keyname=='msrp'))
{
$Product[$c]['Product'][''.$keyname.''] = str_replace(",", "", $value);
}


elseif(isset($keyname) and ($keyname=='parent_cat_id'))
{
$cat_id = $this->Category->findByName($value, array('fields' => 'Category.id'));
if(isset($cat_id) and ($cat_id)):
$Product[$c]['Product'][''.$keyname.''] = $cat_id['Category']['id'];
endif;
}
elseif(isset($keyname) and ($keyname=='brand_id'))
{
$Brand_id = $this->Brand->findByName($value, array('fields' => 'Brand.id'));
if(isset($Brand_id) and ($Brand_id)):
$Product[$c]['Product'][''.$keyname.''] = $Brand_id['Brand']['id'];
endif;
}
elseif(isset($keyname) and ($keyname=='dept_id'))
{
$Department_id = $this->Department->findByName($value,
 array('fields' => 'Department.id'));
if(isset($Department_id) and ($Department_id)){
$Product[$c]['Product'][''.$keyname.''] = $Department_id['Department']['id'];
}else{unset($Product[$c]['Product'][''.$keyname.'']);
}
}
else
{
$Product[$c]['Product'][''.$keyname.''] = $value;
}
}
}

$x++;
}
$c++;
}
if(isset($Product) and ($Product)){
$no=1;
foreach($Product as $Productno)
{
/* echo $no; */
if(isset($Productno['Product']['name']) and ($Productno['Product']['name'])):
$this->data['Product'] = $Productno['Product'];
$this->data['Product']['id'] = "";
$this->data['Product']['upload'] = "1";
$slug = str_replace(" ", "_", $this->data['Product']['name']);
$slug = strtolower($slug);
$this->data['Product']['slug'] = $this->Product->generateSlug($slug);
$this->data['Product']['hash'] = $this->__generateKey('Product');
$this->data['Product']['wisn'] = $this->__generateWisn('Product');
$this->data['Product']['status'] = 1;
$this->data['Product']['tax_type'] = 9;
$this->data['Product']['created_by'] = $this->Auth->User('id');
$this->data['Product']['fullfiled_by'] = "Merchant";
//pr($this->data);
//exit;
$this->Product->Save($this->data['Product']);
$last_id = $this->Product->getLastInsertId();
if(isset($this->data['Product']['img']) and ($this->data['Product']['img']))
{
$this->data['Product']['id'] = $last_id;
$img_name = $this->url_upload('Product', $this->data['Product']['img']);
$this->data['Product']['img'] = $img_name;
$this->Product->Save($this->data['Product']);
}
if(isset($this->data['Product']['rel_img']) and ($this->data['Product']['rel_img']))
{
$exp_rel_images = explode('@@', $this->data['Product']['rel_img']);
//pr($exp_rel_images);
//exit;
//$last_id = 901; 
foreach($exp_rel_images as $exp_rel_image)
{

$img_name = $this->url_upload('Product/'.$last_id.'', $exp_rel_image);
$img_rel_arr[] = $img_name;

}
if(isset($img_rel_arr) and ($img_rel_arr))
{
$this->data['Product']['rel_img'] = implode(',', $img_rel_arr );
$this->data['Product']['id'] = $last_id;
$this->Product->Save($this->data['Product']);
}
}

$this->data['MasterProduct'] = $Productno['MasterProduct'];
if(isset($this->data['MasterProduct']) and ($this->data['MasterProduct']))
{
$this->data['MasterProduct']['id'] = "";
$this->data['MasterProduct']['user_id'] = $this->Auth->User('id');
$this->data['MasterProduct']['seller_id'] = $this->Auth->User('Seller_id');
$this->data['MasterProduct']['product_id'] = $last_id;
$this->data['MasterProduct']['wisn'] = $this->data['Product']['wisn'];
$pr_weight = str_replace(' ', "", $this->data['Product']['weight']);
$pr_weight = str_replace('lb', "", $pr_weight);
$product_defined_wieght = $this->_get_seller_shipping_rate( $pr_weight,
 $this->data['MasterProduct']['seller_id']
);
if(isset($product_defined_wieght) and ($product_defined_wieght))
{
if(isset($this->data['MasterProduct']['shipping_method']) and
($this->data['MasterProduct']['shipping_method']=='MSF'))
{
$this->data['MasterProduct']['shipping_price'] =
($this->data['Product']['weight']*$product_defined_wieght);
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}
}
else
{
$this->data['MasterProduct']['shipping_price'] = 0;
}
$this->MasterProduct->Save($this->data['MasterProduct']);
}
endif;
$no++;
}
}
else
{
$this->Session->setFlash('Please upload products with correct CSV File Format.');
$this->redirect(array('controller' => 'Sellers', 'action' => 'inventory'));
}
}
}

function delete_by_id($model, $id, $action)
{
echo __METHOD__;
if ($this->$model->delete($id))
{
$this->Session->setFlash('The '.$model.' item has been deleted.');

$this->redirect(array('action' => $action));
}
}


function update_inventory()
{
echo __METHOD__;
$this->layout="ajax";
$params = $this->params;
if(isset($params['form']['Main']) and ($params['form']['Main'])):
$action = $params['form']['Main'];
endif;
switch($action)
{
case"Quantity":
$this->data['MasterProduct']['id'] = $params['form']['e_master_id'];
$this->data['MasterProduct']['qty'] = $params['form']['e_qty_val'];
if(isset($this->data['MasterProduct']) and ($this->data['MasterProduct']))
{
$this->MasterProduct->Save($this->data['MasterProduct']);
$this->Set('id', $this->data['MasterProduct']['id']);
$this->Set('Main', $params['form']['Main']);
$this->Set('value', $this->data['MasterProduct']['qty']);
}
break;
case"Price":
$this->data['MasterProduct']['id'] = $params['form']['e_master_id'];
$this->data['MasterProduct']['price'] = $params['form']['e_price_val'];
if(isset($this->data['MasterProduct']) and ($this->data['MasterProduct']))
{
$this->MasterProduct->Save($this->data['MasterProduct']);
$this->Set('id', $this->data['MasterProduct']['id']);
$this->Set('Main', $params['form']['Main']);
$this->Set('value', $this->data['MasterProduct']['price']);
}
break;
}
}

}		


